package com.ynhenc.pta.routeRank.impl;

import com.ynhenc.pta.routeRank.*;

/**
 * @author sbmoon
 * 
 */
public class PathImpl extends ComLib_PTA implements Path {

	public float getTime_Minute() {
		// TODO 경로의 소요 시간 계산
		return 0;
	}

	private VertexList getVertexList() {
		return this.vertexList;
	}

	public void toKml(StringBuffer buff) {
		buff.append("<LineString>");
		buff.append(NL);
		buff.append("<tessellate>1</tessellate>");
		buff.append(NL);
		buff.append("<coordinates>");
		buff.append(NL);
		int size = this.getCountVertex();
		int index = 0;
		for (Vertex vertex : this.getVertexList()) {
			vertex.toKml(buff);
			if (index < size - 1) {
				buff.append(", ");
			}
			index++;
		}
		buff.append(NL);
		buff.append("</coordinates>");
		buff.append(NL);
		buff.append("</LineString>");
	}

	public int getCountVertex() {
		return vertexList.size();
	}

	public void addVertexList(float[] poly) {
		if (poly == null) {
			// do nothing !!!!
		} else {
			int no = poly.length / 2;
			float x, y;
			for (int i = 0, iLen = no; i < iLen; i++) {
				x = poly[2 * i];
				y = poly[2 * i + 1];
				if (i == 0) {
					this.addVertex(new Node(x, y, ""));
				} else if (i < no - 1) {
					this.addVertex(x, y);
				} else {
					this.addVertex(new Node(x, y, ""));
				}
			}
		}
	}

	public void addVertex(double x, double y) {
		this.addVertex(new Vertex(x, y));
	}

	public void addVertex(Vertex vertex) {
		boolean localDebug = false;
		if (localDebug) {
			System.out.println("index(" + this.vertexList.size() + ") " + vertex.toString());
		}
		this.vertexList.add(vertex);
	}

	public Vertex getVertex(int index) {
		return this.vertexList.get(index);
	}

	public Node getNodeStart() {
		Vertex vertex = this.vertexList.getStart();
		if (vertex != null) {
			if (vertex.isNode()) {
				return (Node) vertex;
			} else {
				return new Node("", vertex.getX(), vertex.getY());
			}
		} else {
			return null;
		}
	}

	public Node getNodeEnd() {
		Vertex vertex = this.vertexList.getEnd();
		if (vertex != null) {
			if (vertex.isNode()) {
				return (Node) vertex;
			} else {
				return new Node("", vertex.getX(), vertex.getY());
			}
		} else {
			return null;
		}
	}

	private final VertexList vertexList = new VertexList();

}
